import request from '@/utils/request'
import { ElMessage } from 'element-plus'

export default class BaseApi {
  constructor(apiRouter, gridId) {
    this.apiRouter = apiRouter
    this.gridId = gridId
  }

  requestBase(requestBody) {
    return request({
      ...requestBody,
      headers: {
        gridId: this.gridId
      }
    })
  }

  page({ data, page }) {
    return this.requestBase({
      url: `${this.apiRouter}/page/${page.pageNum}/${page.pageSize}`,
      method: 'post',
      data
    })
  }

  pageNoSql({ page }) {
    return this.requestBase({
      url: `${this.apiRouter}/pageNoSql/${page.pageNum}/${page.pageSize}`,
      method: 'get'
    })
  }

  list() {
    return this.requestBase({
      url: `${this.apiRouter}/list`,
      method: 'get'
    })
  }

  tree(data) {
    return this.requestBase({
      url: `${this.apiRouter}/tree`,
      method: 'post',
      data
    })
  }

  /**
   * 获取单条数据
   * @param id
   * @returns {*}
   */
  get(id) {
    return this.requestBase({
      url: `${this.apiRouter}/get/${id}`,
      method: 'get'
    })
  }

  /**
   * 添加
   * @param data
   * @returns {*}
   */
  add(data) {
    return this.requestBase({
      url: `${this.apiRouter}/add`,
      method: 'post',
      data
    })
  }

  /**
   * 复制
   * @param id
   * @returns {*}
   */
  copy(id) {
    return this.requestBase({
      url: `${this.apiRouter}/copy/${id}`,
      method: 'get'
    })
  }

  /**
   * 编辑 @param data
   * @returns {*}
   */
  edit(data) {
    return this.requestBase({
      url: `${this.apiRouter}/edit`,
      method: 'post',
      data
    })
  }

  /**
   * 删除
   * @returns {*}
   * @param id
   */
  del(id) {
    return this.requestBase({
      url: `${this.apiRouter}/del/${id}`,
      method: 'get'
    })
  }

  /**
   * 上传
   * @returns {*}
   * @param id
   */
  importExcel() {
    return `${import.meta.env.VITE_BASE_URL}${this.apiRouter}/importExcel`
  }

  /**
   * 下载导入模版
   * @returns {*}
   */
  importExcelTemplateDownload(gridId) {
    this.requestBase({
      url: `${this.apiRouter}/importExcelTemplateDownload/${gridId}`,
      method: 'get',
      responseType: 'blob'
    })
      .then((res) => {
        this.downloadFile(res)
      })
      .catch((err) => {
        console.log(err)
        ElMessage.error('下载失败!')
      })
  }

  /**
   * 导入
   * @returns {*}
   * @param id
   */
  importExcelData(importRecotdId) {
    return this.requestBase({
      url: `${this.apiRouter}/importExcelData/${importRecotdId}`,
      method: 'get'
    })
  }

  /**
   * 导出
   * @param {*} param0
   * @returns
   */
  exportExcel({ data, gridId }) {
    return this.requestBase({
      url: `${this.apiRouter}/exportExcel/${gridId}`,
      method: 'post',
      data,
      responseType: 'blob'
    })
      .then((res) => {
        this.downloadFile(res)
      })
      .catch((err) => {
        console.log(err)
        ElMessage.error('下载失败!')
      })
  }

  /**
   * 文件流下载
   * @param {*} res
   */
  downloadFile(res) {
    const { data, headers } = res
    const disposition = headers['content-disposition']
    const fileName = disposition.replace(/\w+;filename=(.*)/, '$1')
    const blob = new Blob([data], { type: headers['content-type'] })
    let dom = document.createElement('a')
    let url = window.URL.createObjectURL(blob)
    dom.href = url
    dom.download = decodeURI(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom.parentNode.removeChild(dom)
    window.URL.revokeObjectURL(url)
  }

  /**
   * 移动
   * @param {*} type 类型(top:向上移动,bottom:向下移动,topMost:移动到最前,bottomMost:移动到最后)
   * @param {*} id
   * @returns
   */
  move(type, id) {
    return this.requestBase({
      url: `${this.apiRouter}/move/${type}/${id}`,
      method: 'get'
    })
  }

  /**
   * 树形_移动
   * @param {*} type 类型(top:向上移动,bottom:向下移动,topMost:移动到最前,bottomMost:移动到最后)
   * @param {*} id
   * @returns
   */
  treeMove(type, id) {
    return this.requestBase({
      url: `${this.apiRouter}/treeMove/${type}/${id}`,
      method: 'get'
    })
  }
}
